<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command\ocean;

use app\command\BaseWork;
use app\enum\OeInvalidVideoEnum;
use app\model\OeInvalidVideoTaskLogModel;
use app\model\OeInvalidVideoTaskModel;
use think\console\Input;
use think\console\Output;

class InvalidVideoCleanupTaskStatusWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('oe_invalid_video_cleanup_task_status_work');
        // 设置参数
        $this->setDescription('更新清理巨量无效素材任务状态');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        ini_set('memory_limit', '-1');
        $this->getOutputMessage('更新清理巨量无效素材任务状态开始');
        $runTime = time() + 5 * 60;

        while (time() < $runTime) {
            // 获取状态未更新完毕的记录
            $results = (new OeInvalidVideoTaskModel())->where([
                ['data_ym', '=', date('Ym')],
                ['task_status', '=', OeInvalidVideoEnum::CLOSE_STATUS_SCHEDULE],
            ])->field('id,all_num')->select();
            foreach ($results as $result) {
                $finished = true;
                $success_num = 0;
                $finish_num = 0;
                if ($result->getAttr('all_num') > 0) {
                    $objs = OeInvalidVideoTaskLogModel::alias('a')
                        ->leftJoin(['habo_oe_invalid_video' => 'b'], 'a.content_id=b.id')
                        ->where(['a.task_id' => $result->getKey()])
                        ->field('b.close_status')
                        ->select()->toArray();

                    foreach ($objs as $obj) {
                        if ($finished && $obj['close_status'] == OeInvalidVideoEnum::CLOSE_STATUS_START) {
                            $finished = false;
                        }
                        if ($obj['close_status'] == OeInvalidVideoEnum::CLOSE_STATUS_SUCCESS) {
                            ++$success_num;
                        }
                        if (! in_array($obj['close_status'], [OeInvalidVideoEnum::CLOSE_STATUS_START, OeInvalidVideoEnum::CLOSE_STATUS_SCHEDULE])) {
                            ++$finish_num;
                        }
                    }
                }

                $update = ['success_num' => $success_num];
                if ($finished && in_array($result->getAttr('all_num'), [$finish_num, 0])) {
                    $update['task_status'] = OeInvalidVideoEnum::CLOSE_STATUS_SUCCESS;
                }
                $result->setAttrs($update);
                if ($result->save()) {
                    $this->getOutputMessage('任务[' . $result->getKey() . ']更新数量和状态完成');
                } else {
                    $this->getOutputMessage('任务[' . $result->getKey() . ']更新数量和状态失败');
                }
            }
            sleep(rand(5, 10));
        }

        $this->getOutputMessage('更新清理巨量无效素材任务状态结束');
    }
}
